package com.yonyou.crm.sys.region.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.sys.region.entity.RegionsVO;

@Service
public class RegionsBizServiceImpl  extends BaseBizServiceImpl<RegionsVO> {
	public List<Map<String, Object>> getRegionCityDistrictData(){
		return dao.selectList(RegionsVO.class,"selectRegions");
	}
	
	public Object selectFieldsByIds(Object[] ids) {
		Map<String, Object> para = new HashMap<String, Object>();
		para.put("ids", ids);
		return dao.selectList(RegionsVO.class, "idGet", para);
	}

    public void syncRegionJW() {
		List<Map<String, Object>> syncRegionList = dao.selectList(RegionsVO.class,"selectRegionForSyncJW");
		int i = 0;
		for(Map<String, Object> map : syncRegionList) {
			String name = map.get("name") == null ? "" : map.get("name").toString();
			String pname = map.get("pname") == null ? "" : map.get("pname").toString();
			String id = map.get("id").toString();
			BufferedReader in = null;
			try {
				name = URLEncoder.encode(name, "UTF-8");
				pname = URLEncoder.encode(pname, "UTF-8");
				String appkey = "WGs4SVG7d9sH2t6ovqzCvpx8aaPv6RXK";
				if(i < 1000){
					appkey = "zRjTrKGWXOV03893RH56Hzyw";
				}
				String httpUrl = "http://api.map.baidu.com/place/v2/search?query=" + name + "&region=" + pname + "&output=json&ak="+appkey;
				URL url = new URL(httpUrl);
				in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
				String res;
				StringBuilder sb = new StringBuilder("");
				while ((res = in.readLine()) != null) {
					sb.append(res.trim());
				}
				String str = sb.toString();
				ObjectMapper mapper = new ObjectMapper();
				JsonNode readTree = mapper.readTree(str);
				if (readTree.findValue("status").asText().equals("0")) {
					JsonNode jwJsonValue = readTree.findValue("location");
					Thread.sleep(2*1000);
					i++;
					if(jwJsonValue == null ){ continue;}
					JsonNode lat = jwJsonValue.findValue("lat");
					JsonNode lng = jwJsonValue.findValue("lng");
					Map<String,Object> param = new HashMap<>();
					param.put("id", new Long(id));
					param.put("lat", lat.toString());
					param.put("lng", lng.toString());
					dao.updateSql(RegionsVO.class, "syncJW", param);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public List<RegionsVO> getRefervaluesForApp(Page<RegionsVO> page, Map<String, Object> paraMap) {
		Page<RegionsVO> pageVO = dao.pageSelect(RegionsVO.class, page, "selectRefervalues", "countRefervalues", paraMap);
		List<RegionsVO> voList = pageVO.getData();
		return voList;
	}
    
}
